package com.redstar.expenses;

import java.util.Currency;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ExpenseListAdapter extends BaseAdapter{
	private Context context;
	private List<Expense> expenses;
	
	public ExpenseListAdapter(Context context, List<Expense> expenses){
		this.context = context;
		this.expenses = expenses;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return expenses.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return expenses.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup viewGroup) {
		// TODO Auto-generated method stub
		Expense entry = expenses.get(position);
		Currency money = Currency.getInstance(entry.getCurrencyCode());
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.expense_row, null);
        }		
        
        TextView DataAndTimeTV = (TextView) convertView.findViewById(R.id.DateAndTimeTV);
        DataAndTimeTV.setText(entry.getTimeDateString());
        
        TextView ShopTV = (TextView) convertView.findViewById(R.id.ShopTV);
        ShopTV.setText(entry.getShop());
        
        TextView CostTimeTV = (TextView) convertView.findViewById(R.id.CostTV);
        if(entry.getCost()==-1){
        	CostTimeTV.setText(money.getSymbol());
        }else{        
        	CostTimeTV.setText(String.format(money.getSymbol() + "%." + money.getDefaultFractionDigits() + "f", entry.getCost()));
        }
        
        TextView ItemTV = (TextView) convertView.findViewById(R.id.ItemTV);
        ItemTV.setText(entry.getItem());
        return convertView;        
	}
}
